#!/bin/sh
#description: Weblogic start-stop script.
#by oilive
#DOMAN_HOME=weblgogic实例安装路径
DOMAIN_HOME=/weblogic/cpsweb/user_projects/domains/cdomain/
#启停用户
WLS_OWNER=weblogic
#日志输出目录
LOGS_HOME=/weblogic/cpslogs/
#日志备份目录
LOGS_BACKHOME=/weblogic/cpslogs/logbacks/
#域访问地址
DOMAIN_URL=http://127.0.0.1:7001
#备份后缀名
d=`date "+%Y%m%d%H%M%S"`
nodes=(AdminServer server1 server2)

#启动任意节点
function on(){
	if [ ! -d "${LOGS_BACKHOME}" ];then
		mkdir ${LOGS_BACKHOME}
	fi
	if [ -f "${LOGS_HOME}${1}.out" ];then
		mv ${LOGS_HOME}${1}.out ${LOGS_BACKHOME}${1}-${d}.out
	fi
	if [ ${1} = "admin" ];then
		su - ${WLS_OWNER} -c "cd ${DOMAIN_HOME}; nohup ./startWebLogic.sh >> ${LOGS_HOME}admin.out 2>&1 &"
	else
		su - ${WLS_OWNER} -c "cd ${DOMAIN_HOME}bin/; nohup ./startManagedWebLogic.sh ${1} ${DOMAIN_URL} >> ${LOGS_HOME}${1}.out 2>&1 &"
	fi
	echo -e "! Weblogic \033[31m${1}\033[0m \033[32mstarting\033[0m, please wait a moment!!!"
}
#启动服务节点
function startNode(){
	chown -R ${WLS_OWNER}:${WLS_OWNER} /home/weblogic/
	chown -R weblogic:weblogic /weblogic/
	echo "!---------------------------------------------!"
	num=`ps -ef |grep java |grep -v grep |grep AdminServer |wc -l` 
	if [[ ${num} -eq 0 ]];then
		echo -e "! Weblogic \033[31mAdminNode\033[0m \033[33mnot started\033[0m!!!"
		echo -e "! so must be \033[33mstart\033[0m \033[31mAdminNode\033[0m \033[32mfirst\033[0m!!!"
	else 
		num=`ps -ef|grep weblogic|grep =${1}|awk '{print $2}'`
		if [[ ${num} -eq 0 ]];then
			on ${1}
		else
			echo -e "! weblogic \033[31m${1}\033[0m is \033[32mrunning\033[0m, Can't \033[33mstart again\033[0m!!!"
		fi
	fi
	echo "!---------------------------------------------!"
}
#停止节点
function stopNode(){
	num=`ps -ef |grep java |grep -v grep |grep ${1} |wc -l` 
	if [ ${num} -eq 0 ];then
		echo -e "! Weblogic \033[31m${1}\033[0m \033[33mnot start\033[0m, so \033[33mno\033[0m need \033[31mstop\033[0m!!!" 
	else
		WLS_PID=`ps -ef|grep weblogic|grep =${1}|awk '{print $2}'`
		kill -9 $WLS_PID
		echo -e "! Weblogic \033[31m${1}\033[0m \033[33mstopped\033[0m \033[32msuccessfully\033[0m!!!" 
	fi
}
#输出日志
function out(){
	tail -100f ${LOGS_HOME}${1}.out
}
#查看节点状态
function status(){
	echo "!---------------------------------------------!"
	num=`ps -ef |grep java |grep -v grep |grep ${1} |wc -l` 
	if [ ${num} -eq 0 ];then
		echo -e "! Weblogic \033[31m${1}\033[0m \033[33mnot start\033[0m, so \033[33mdoesn't\033[0m have \033[31m${1}_process\033[0m!!!" 
	else
		echo -e "! Weblogic \033[31m${1}\033[0m \033[33mprocess\033[0m \033[32mshow successed\033[0m!!!" 
		ps -ef|grep weblogic|grep =${1}
	fi
	echo "!---------------------------------------------!"
}
#清除缓存
function clean(){
	judge ${1}
	echo "!---------------------------------------------!"
	cache_dir=${DOMAIN_HOME}servers/${1}
	rm -rf ${cache_dir}/tmp/*
	rm -rf ${cache_dir}/cache/*
	echo -e "! Weblogic \033[31m${1} Cache\033[0m \033[33mclean\033[0m \033[32msuccessed\033[0m!!!"
	echo "!---------------------------------------------!"
}
#转换主节点名称
function switch(){
	node=$1
	case "$1" in
	'admin')
		node="AdminServer"
	;;
	esac
	echo $node
}
#判断节点进程存在则不能继续执行
function judge(){
	num=`ps -ef |grep java |grep -v grep |grep $node |wc -l` 
	if [[ ${num} -ne 0 ]];then 
		echo "!---------------------------------------------!"
		echo -e "! Weblogic \033[31m${node}\033[0m is \033[32mrunning\033[0m, Can't \033[33mUnable to perform other operations\033[0m!!!"
		echo "!---------------------------------------------!"
		exit 1
	fi
}
#使用说明，用来提示输入参数
function usage() {
    echo "请使用: "
	echo "$0 start [all|admin|server1|server2]用于启动节点！"
	echo "$0 stop [all|admin|server1|server2]用于关闭节点！"
	echo "$0 restart [all|admin|server1|server2]用于重新启动节点！"
	echo "$0 ps [all|admin|server1|server2]用于查看节点进程！"
	echo "$0 tail [admin|server1|server2]用于查看节点日志！"
	echo "$0 clean [all|admin|server1|server2]用于清除节点缓存！"
    exit 1
}
#根据节点记录循环执行$1方法
function circle(){
	num=${#nodes[@]}
	for((i=0;i<$num;i++));do
		$1 nodes[i]
	done
}
#当参数中包含all时调用
function action(){
	case "$1$2" in
	'startall')
		circle startNode
	;;
	'stopall')
		circle stopNode
	;;
	'restartall')
		circle stopNode
		circle startNode
	;;
	'statusall')
		circle status
	;;
	'cleanall')
		circle clean
	;;
	esac
	exit 1
}
case "$1" in
'start')
	node=$(switch $2)
	judge $node
	action start $2
	startNode $node
;;
'stop')
	node=$(switch $2)
	action stop $2
	stopNode $node
;;
'restart')
	node=$(switch $2)
	action restart $2
	stopNode $node
	startNode $node
;;
'ps')
	node=$(switch $2)
	action status $2
	status $node
;;
'tail')
	out $2
;;
'clean')
	node=$(switch $2)
	action clean $2
	clean $node
;;
*)
	usage
;;
esac